require "Common/define"
require "Logic/Config"
require "Common/functions"

UIDrawResult = {};
HelperFunc.SetUIEnv(UIDrawResult)
local this = UIDrawResult;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;

this.cardBG = nil;
this.cardBG2 = nil;
this.btnContinue = nil;
this.qualitybtm = nil;
this.aniFail = nil;
this.fail_Single = nil;
this.aniReset = nil;
this.hasInit = false;
this.curShowCard = nil;
this.Rewardlist = {};
this.bgName = "beijingtu";
this.bgName2 = "bantoumingdi";
this.tab_quality = Config.IconQuality
this.tab_frame = Config.IconFrame
this.cardBgItems = {}
this.cardItems = {}

function UIDrawResult.Init(cards, type, param)
    this.curShowCard = cards;
    this.resultType = type;
    this.typeParam = param;
    local name, packageName = getUIName(PanelNames.UIDrawResult);
    panelMgr:ShowUI(name, packageName, this.onCreate, Config.useLocalUI);
end

function UIDrawResult.onCreate(obj)
    if not hasInit then
        this.hallowAudioValue = 0
        AddUIPackageDepend(PanelNames.UICharactorMain, "FUI/HalfBodyAtlas")
        hasInit = true;
        --btnContinue = obj:GetChild("btncontinue");
        this.again = obj:GetChild("again");
        this.skip = obj:GetChild("skip");
        this.btnSure = this.again:GetChild("btnSure");
        this.btnTenMore = this.again:GetChild("btnTenMore");
        this.againIcon = this.again:GetChild("icon");
        this.againNum = this.again:GetChild("num");
        this.againText_Times = this.again:GetChild("Text_Times");
        this.againBtn_Continue = this.again:GetChild("Btn_Continue");
        this.againBtn_Record = this.again:GetChild("Btn_Record");
        this.againBtn_BackButton = this.again:GetChild("Btn_BackButton");
        this.againAllSkipButton = this.again:GetChild("allSkip");
        this.againAllSkipTips = this.again:GetChild("allSkipTips");
        this.againAllSkipTips.text = getLanguage("Draw_SkipAni");
        this.againDrawNumController = this.again:GetController("drawNum");
        this.againTypeController = this.again:GetController("type");
        this.againIsSkipController = this.again:GetController("isSkip");
        aniFail = obj:GetTransition("fail");
        fail_Single = obj:GetTransition("fail_Single");
        aniReset = obj:GetTransition("showCountinue");
        this.cardReset = obj:GetTransition("reset");
        this.showCardTransition = obj:GetTransition("showCard");
        this.btn_share = obj:GetChild("n140");
        this.ctr_share = obj:GetController("share")

        --this.CardDown = obj:GetTransition("CardDown");
        --this.CardDownSingle = obj:GetTransition("CardDownSingle");
        --this.spinsList = {};
        --for i = 1, 11 do
        --    table.insert(this.spinsList, obj:GetChild("spine" .. i));
        --end
        this.spinsTen = obj:GetChild("spine1");
        this.spinsOne = obj:GetChild("spine11");
        this.drawNumController = obj:GetController("drawNum");
        cardItems = {};

        for i = 1, 11 do
            local carditem = obj:GetChild("herobg" .. i);
            table.insert(cardItems, carditem);
        end
        cardBgItems = {};
        for i = 1, 10 do
            local carditem = obj:GetChild("Effect_ChouKa" .. i);
            table.insert(cardBgItems, carditem);
        end
        this.ctrlItem = CtrlManager.GetCtrl(PanelNames.HeadRectBItem);
        this.addEvent();
    end
    this.refreshCard(nil, nil, nil, true);
    logicMgr.GuideMgr.Pause()
end

function UIDrawResult.addEvent()
    this.btnSure.onClick:Add(this.onClickContinue);
    this.btnTenMore.onClick:Add(this.onClickAgain);
    this.skip.onClick:Add(this.onClickSkip);
    this.againBtn_Record.onClick:Add(this.onClickRecord);
    this.againBtn_BackButton.onClick:Add(this.onClickContinue);
    this.againBtn_Continue.onClick:Add(this.onClickSelectContinue);
    this.againAllSkipButton.onClick:Add(this.onClickAllSkip);
    this.btn_share.onClick:Add(this.OnClickShare);
    ListenEvent(Config.EventType.Fresh_SelectDrawSave, this.updateAgain);
end

function UIDrawResult.removeEvent()
    this.btnSure.onClick:Remove(this.onClickContinue);
    this.btnTenMore.onClick:Remove(this.onClickAgain);
    this.skip.onClick:Remove(this.onClickSkip);
    this.againBtn_Record.onClick:Remove(this.onClickRecord);
    this.againBtn_BackButton.onClick:Remove(this.onClickContinue);
    this.againBtn_Continue.onClick:Remove(this.onClickSelectContinue);
    this.againAllSkipButton.onClick:Remove(this.onClickAllSkip);
    CloseEvent(Config.EventType.Fresh_SelectDrawSave, this.updateAgain);
end

function UIDrawResult.OnDestroy()
    this.hasInit = false;
    if this.resetTime then
        utimer.remove(this.resetTime);
        this.resetTime = nil;
    end
    this.removeEvent();
    if UIPackage.GetById("FUI/HalfBodyAtlas") then
        UIPackage.RemovePackage("FUI/HalfBodyAtlas")
    end
end

function UIDrawResult.OnEnable()
    this.hallowAudioValue = 0
    AudioManager.SetRTPCValue("hallow_house", this.hallowAudioValue)
end

function UIDrawResult.OnDisable()
    this.hallowAudioValue = 0
    AudioManager.SetRTPCValue("hallow_house", this.hallowAudioValue)
    AudioManager.RefreshBGM()
end

function UIDrawResult.Settypecon()
    this.againTypeController.selectedIndex=3;
end

function UIDrawResult.onClickAgain()
    if not this.notAgain then
        return ;
    end
    print("抽卡有效再次十连");
    this.notAgain = nil;
    this.hallowAudioValue = 0
    AudioManager.SetRTPCValue("hallow_house", this.hallowAudioValue)
    this.cardEndReset(function ()
        UIDraw.drawSkills();
        if this.drawAgainTime then
            utimer.remove(this.drawAgainTime);
            this.drawAgainTime = nil;
        end
        this.drawAgainTime = utimer.timeCall(function ()
            this.againTypeController.selectedIndex = 4;
        end , 3000, 1);
    end);
end

function UIDrawResult.onClickContinue()
    this.hallowAudioValue = 0
    AudioManager.SetRTPCValue("hallow_house", this.hallowAudioValue)
    --logicMgr.HeroManager.showDrawWnd(#this.curShowCard)
    logicMgr.HeroManager.showDrawInfoWnd(#this.curShowCard)
    this.cardEndReset(function ()
        this.onCloseUIDrawResult();
    end);
end

function UIDrawResult.onCloseUIDrawResult()
    DestoryUI(PanelNames.UIDrawResult)
end

function UIDrawResult.updateCard(index, itemObj, itemData, getHeroSource)
    local heroid = itemData.itemModelId or itemData.modelId;
    local heroconfig = G.dataTable["t_hero"][tostring(heroid)];
    if heroconfig ~= nil then
        if heroconfig.f_HeroInitialStar >= 40 then
            this.hallowAudioValue = 1
        end
        local data = {}
        logicMgr.HeroManager.copyHeroConfig(heroconfig, data);
        this.heroData = logicMgr.HeroManager.getHeroDatabyModelId(data.partnerModelId,true);
        if this.heroData == nil then
            logError("抽卡找不到自己的英雄t_hero f_HeroID:"..tostring(data.partnerModelId));
        end
        if tonumber(data.star) >= 60 then
            this.starItems[index] = "SSR"
            this.ssrNum = this.ssrNum + 1;
            if data.faction == 1 then
                this.raceItems[index] = "SSR_Shen";
            elseif data.faction == 2 then
                this.raceItems[index] = "SSR_Mo";
            elseif data.faction == 3 then
                this.raceItems[index] = "SSR_Qw";
            else
                this.raceItems[index] = "SSR";
            end
        elseif tonumber(data.star) >= 40 then
            this.starItems[index] = "SR"
            this.raceItems[index] = "SR";
        elseif tonumber(data.star) >= 20 then
            this.starItems[index] = "R"
            this.raceItems[index] = "R";
        else
            this.starItems[index] = "N"
            this.raceItems[index] = "N";
        end
        if this.drawBehavior.notGetHero then
            if this.heroData.partnerId == 0 and tonumber(data.star) > 10 then
                data.isNewHero = true;
            else
                data.isNewHero = nil;
            end
        else
            this.needResolveList[index] = false;
            if this.heroData ~= nil and this.heroData.isNew then
                data.isNewHero = true;
                this.isNewHero[index] = data.partnerModelId;
                logicMgr.HeroManager.setHeroIsNew(this.heroData.partnerId, false, getHeroSource)
            else
                data.isNewHero = nil;
                this.isNewHero[index] = nil;
                this.needResolveList[index] = true;
                logicMgr.CostManager.RefreshItem(itemObj:GetChild("convertnum"):GetChild("item"), {heroconfig.f_StoneId});
                itemObj:GetChild("convertnum"):GetChild("convertnum").text = heroconfig.f_RepeatCount;
                if tonumber(heroconfig.f_StoneId)~=0 then --过滤新英雄情况
                    if this.heroClips[heroconfig.f_StoneId] then
                        this.heroClips[heroconfig.f_StoneId]=this.heroClips[heroconfig.f_StoneId]+ heroconfig.f_RepeatCount
                    else
                        this.heroClips[heroconfig.f_StoneId]=heroconfig.f_RepeatCount
                    end
                end

            end
            this.isNeedResolve = false;
            if tonumber(data.star) <= 10 then
                this.isNeedResolve = true;
            elseif this.heroData ~= nil and this.heroData.star >= this.heroMaxStar then
                this.isNeedResolve = true;
            end
            if this.isNeedResolve then
                for k, v in pairs(decodeJsonStrToTabel(heroconfig.f_AutoTrans)) do
                    table.insert(this.resolveItems, v);
                    logicMgr.CostManager.RefreshItem(itemObj:GetChild("convertnum"):GetChild("item"), {v[1]});
                    itemObj:GetChild("convertnum"):GetChild("convertnum").text = v[2];
                end
                this.needResolveList[index] = true;
            end
        end
        local ifWishController = itemObj:GetController("ifWish");
        ifWishController.selectedIndex = 0;
        if this.showWishFlag then
            for k, v in pairs(this.wishList) do
                if heroid == v then
                    ifWishController.selectedIndex = 1;
                    table.remove(this.wishList, k);
                    break
                end
            end
        end
        if itemObj:GetChild("Bg_SR") ~= nil and itemObj:GetChild("Bg_SSR") ~= nil and itemObj:GetChild("Bg_R") ~= nil then
            itemObj:GetChild("Bg_SR").visible = false;
            itemObj:GetChild("Bg_SSR").visible = false;
            itemObj:GetChild("Bg_R").visible = false;
        end
        if this.ctrlItem ~= nil then
            this.ctrlItem.initData(itemObj, data);
        end
    else
        logError("抽卡找不到该英雄t_hero f_HeroID:"..tostring(heroid));
    end
end

function UIDrawResult.RefreshShareCtr()
    if isOpenUI(PanelResNames.UISpecialDraw) then--屏蔽先知召唤
        this.ctr_share.selectedIndex = 0
        return
    end

    local count = table.getn(this.curShowCard)
    if count == 1 then
        local config = G.dataTable["t_Hero"][tostring(this.curShowCard[1].itemModelId)]
        if config and config.f_HeroInitialStar >= 60 then
            this.ctr_share.selectedIndex = 1
        else
            this.ctr_share.selectedIndex = 0
        end
    else
        local cf 
        local ssr_num = 0
        for k,v in pairs(this.curShowCard) do
            cf = G.dataTable["t_Hero"][tostring(v.itemModelId)]
            if cf and cf.f_HeroInitialStar >= 60 then
                ssr_num = ssr_num + 1
            end
        end
        if ssr_num <= 1 then
            this.ctr_share.selectedIndex = 0
        else
            this.ctr_share.selectedIndex = 1
        end
        
    end
end

---@param type number 值为nil, 代表正常抽卡流程,有卡片飞入同时顺序翻转,有资源转换,有新英雄展示
---@param type number 值为1, 代表正常连斩流程,有卡片飞入同时顺序翻转,无资源转换,无新英雄展示
---@param type number 值为2, 代表连斩再次进入时,无卡片飞入同时全部翻转,无资源转换,无新英雄展示
---@param type number 值为3, 代表连斩购买后再次展示时,无卡片飞入同时全部翻转,有资源转换,有新英雄展示
---@param type number 值为4, 代表抽卡有一键跳过时,无卡片飞入同时全部翻转,有资源转换,有新英雄展示
function UIDrawResult.refreshCard(cards, type, param, isFrist)
    if cards ~= nil then
        this.curShowCard = cards;
        this.resultType = type;
        this.typeParam = param;

        this.ctr_share.selectedIndex = 0
    end
    dump(this.curShowCard, "抽卡英雄", 5);
    --this.curShowCard = {};
    --local heroList = {204, 19, 204, 1, 206, 1, 26, 16, 204, 35}
    --for i=1, 10 do
    --    local data = {};
    --    data.itemModelId = heroList[i];
    --    table.insert(this.curShowCard, data);
    --end
    this.heroMaxStar = G.dataTable["t_global"][tostring(93)].f_int_value;
    this.resolveItems = {};
    this.heroClips={}
    this.starItems = {};
    this.raceItems = {};
    this.isNewHero = {};
    this.needShowHeroList = {};
    this.needResolveList = {};
    this.isSkip = nil;
    this.ssrNum = 0;
    this.skip.visible = false;
    local type = 1;
    this.selectDrawFinish = nil;
    this.again.visible = false;
    this.spinsTen.visible = false;
    this.spinsOne.visible = false;
    this.againTypeController.selectedIndex = 0;
    this.drawBehavior = {};
    this.againIsSkipController.selectedIndex = 0;
    if this.drawAgainTime then
        utimer.remove(this.drawAgainTime);
        this.drawAgainTime = nil;
    end
    print("进入状态转换")
    if this.aniFailUTime then
        utimer.remove(this.aniFailUTime);
        this.aniFailUTime = nil;
    end
    if this.aniFailOneUTime then
        utimer.remove(this.aniFailOneUTime);
        this.aniFailOneUTime = nil;
    end
    if this.resultType == nil and logicMgr.HeroManager.getPlayerDrawIsShip() then
        this.resultType = 4;
        this.againIsSkipController.selectedIndex = 1;
    end
    if this.resultType == 2 or this.resultType == 3 or this.resultType == 4 then
        this.drawBehavior.noCardFly = true;
    end
    if this.resultType == 1 or this.resultType == 2 then
        this.drawBehavior.notGetHero = true;
    end
    this.showWishFlag = false;
    if UIDraw and UIDraw.curPoolId == "1" then
        if logicMgr.HeroManager.WishIsGet() then
            local value = tonumber(GetPlayerPrefs("wishToggle"))
            if (not value) or (value and value == 1) then
                this.showWishFlag = true;
            end
        else
            this.showWishFlag = true;
        end
    end
    if this.showWishFlag then
        this.wishList = logicMgr.HeroManager.GetWishData(true);
    end
    print("进入抽卡 卡数:"..(#this.curShowCard))
        -- 单抽
    if #this.curShowCard == 1 then
        print("进入单抽")
        --单抽卡片进场音效
        AudioManager.Play(4006)
        this.drawNumController.selectedIndex = 1;
        this.againDrawNumController.selectedIndex = 1;
        this.updateCard(1, cardItems[1], this.curShowCard[1],TAManager.GetHeroSource.Diamond_One);
        this.spinsOne.visible = true;
        this.spinsOne.alpha = 1;
        Util.PlayAction(this.spinsOne.wrapTarget, "idle1", false);
        fail_Single.ignoreEngineTimeScale = false;
        fail_Single:Play();
        fail_Single.invalidateBatchingEveryFrame = true;
        this.aniFailOneUTime = utimer.timeCall(function ()
            this.aniFailOneUTime = nil;
            this.spinsOne.alpha = 0;
            this.openCardIndex = 1;
            this.openedCardCount = 0;
            this.showOpenCard();
        end, 1300 + (isFrist and 200 or 0), 1);
    else
        print("十连")
        -- 十连
        --十连卡片进场音效
        AudioManager.Play(4007)

        type = 2;
        this.drawNumController.selectedIndex = 0;
        this.againDrawNumController.selectedIndex = 0;
        -- 填充卡片数据
        for i = 1 ,#this.curShowCard do
            this.updateCard(i, cardItems[i], this.curShowCard[i],TAManager.GetHeroSource.Diamond_Ten)
        end
        -- 连斩召唤直接翻转,无卡片进场动效
        -- 无卡片飞舞的一定不能顺序翻转.
        if this.drawBehavior.noCardFly then
            utimer.timeCall(function ()
                this.showCardTransition.ignoreEngineTimeScale = false;
                this.showCardTransition:Play();
                this.showCardTransition.invalidateBatchingEveryFrame = true;
                this.spinsTen.visible = false;
                this.isSkip = false;
                this.openCardIndex = 1;
                this.openedCardCount = 0;
                this.onClickSkip(nil, 2);
            end, 100);
        else
            -- 卡牌入场动效
            aniFail.ignoreEngineTimeScale = false;
            aniFail:Play();
            aniFail.invalidateBatchingEveryFrame = true;
            this.aniFailUTime = utimer.timeCall(function ()
                this.aniFailUTime = nil;
                --this.spinsTen.visible = false;
                this.isSkip = false;
                this.skip.visible = true;
                this.openCardIndex = 1;
                this.openedCardCount = 0;
                print("抽卡开始 ");
                print("抽卡index == ", this.openCardIndex);
                print("抽卡Count == ", this.openedCardCount);
                this.showOpenCard();
            end, 1600 + (isFrist and 300 or 0), 1);
        end
        -- 连斩召唤直接翻转,无卡片进场特效
        if this.drawBehavior.noCardFly then
            this.spinsTen.visible = false;
        else
            this.spinsTen.visible = true;
            Util.PlayAction(this.spinsTen.wrapTarget, "idle2", false);
        end
    end
    -- 处理再次抽卡状态
    -- 连斩召唤
    if this.resultType == 1 or this.resultType == 2 then
        this.againBtn_Record.title = getLanguage("OptionalPool_Btn_Record");
        this.againTypeController.selectedIndex = 1;
        this.againText_Times.visible = false;
        this.againBtn_Continue.title = getLanguage("ActivityDesc1040103");
        this.selectDrawFinish = true;
        this.selectTableData, this.selectServerData = logicMgr.ActiveManager.getActiveData(this.typeParam);
        if this.selectTableData == nil or this.selectServerData == nil then
            this.againTypeController.selectedIndex = 2;
        elseif this.selectServerData.param.selectDrawTimes < this.selectTableData.f_10DrawTime then
            this.againText_Times.visible = true;
            this.againText_Times.text = string.format("%d/%d", this.selectTableData.f_10DrawTime - this.selectServerData.param.selectDrawTimes, this.selectTableData.f_10DrawTime);
            this.againBtn_Continue.title = getLanguage("OptionalPool_Btn_Continue");
            this.selectDrawFinish = nil;
        end
    elseif this.resultType == 3 then
        this.againTypeController.selectedIndex = 3;
    else
        -- 普通十连
        this.againTypeController.selectedIndex = 0;
        if isOpenUI(PanelNames.UIDraw) then
            UIDraw.drawShow(this.againNum, this.againIcon, type);
        end
    end
    AudioManager.SetRTPCValue("hallow_house", this.hallowAudioValue)
    logicMgr.HeroManager.addDrawNeedShowItems(this.resolveItems,this.heroClips);
end

-- 播放单卡
function UIDrawResult.showOpenCard()
    -- 销毁新英雄立绘展示界面
    DestoryUI(PanelNames.UIDrawAnimation);
    -- 如果已经播完
    if this.openedCardCount >= #this.curShowCard then
        print("抽卡结束1 ");
        print("抽卡新英雄展示结束1 ");
        print("抽卡显示再抽一次1 ");
        this.notAgain = true;
        -- if isOpenUI(PanelNames.UIDraw) then
        --     UIDraw.DelayEnableDraw();
        -- end
        -- 再抽一次显示
        this.again.visible = true;
        -- 跳过隐藏
        this.skip.visible = false;
        UIDrawResult.RefreshShareCtr()
        -- 播放再抽一次动效
        aniReset:Play();
        this.showConvert();
        logicMgr.GuideMgr.Resume()
        this.cheakDrawTips();
        return ;
    end
    if this.openCardIndex > #this.curShowCard then
        return ;
    end
    -- 播放卡片背景显隐动效
    cardBgItems[this.openCardIndex]:GetTransition(this.raceItems[this.openCardIndex]):Play()
    -- 播放卡片背景光效spine
    this.showCardBGSpine(cardBgItems[this.openCardIndex]:GetChild(this.raceItems[this.openCardIndex]).wrapTarget, this.starItems[this.openCardIndex], cardBgItems[this.openCardIndex]:GetChild("Effect_"..this.raceItems[this.openCardIndex]), this.raceItems[this.openCardIndex]);
    -- 加入新英雄播放列表
    if this.isNewHero[this.openCardIndex] then
        table.insert(this.needShowHeroList, this.isNewHero[this.openCardIndex]);
    end
    -- 播放卡片反转显隐动效
    local cardItem = cardItems[this.openCardIndex];
    cardItem:GetTransition(this.starItems[this.openCardIndex]):Play(function()
        -- 如果没有点跳过,则继续播放新英雄
        if not this.isSkip then
            this.openedCardCount = this.openedCardCount + 1;
            print("抽卡Count2 == ", this.openedCardCount);
            this.setCardItemAlpha(cardItem);
            this.showOneNewHero(this.showOpenCard);
            print("抽卡新英雄展示1 ");
        else
            this.skipEnd(cardItem);
        end
    end)
    -- 更新当前需要播放的index
    this.openCardIndex = this.openCardIndex + 1
    print("抽卡index1 == ", this.openCardIndex);
end

-- 播放卡片背景光效spine
function UIDrawResult.showCardBGSpine(spine, cardBg, bgSpine, raceType)
    local _cardAudio = AudioManager.Play_ani_card_R
    local index = 4;
    if cardBg == "SSR" then
        _cardAudio = AudioManager.Play_ani_card_SSR
        local idleName2 = "idle3";
        local idleName = "idle";
        if raceType ~= "SSR" then
            idleName2 = "idle";
            idleName = "idle"
        end
        utimer.timeCall(function()
            Util.PlayAction(spine, idleName2, false);
        end, 2100, 1);

        utimer.timeCall(function()
            Util.PlayAction(bgSpine.wrapTarget, idleName, false);
            AudioManager.Play(_cardAudio)
        end, 700, 1);
    else
        if cardBg == "N" then
            index = 4;
        elseif cardBg == "R" then
            index = 1;
        elseif cardBg == "SR" then
            _cardAudio = AudioManager.Play_ani_card_SR
            index = 2;
        end
        Util.PlayAction(spine, "idle"..index, false);
        AudioManager.Play(_cardAudio)
    end

    --播放抽卡音效
    -- AudioManager.Play(_cardAudio)
end

-- 点击一键翻卡
function UIDrawResult.onClickSkip(btn, type)
    type = type or 1;
    print("抽卡一键翻转来源:"..type)
    if this.isSkip == nil or this.openCardIndex > #this.curShowCard then
        print("抽卡一键翻转过滤1:"..this.openCardIndex)
        print("抽卡一键翻转过滤2:"..#this.curShowCard)
        dump(this.isSkip, "抽卡一键翻转过滤3:")
        return;
    end
    print("抽卡一键翻转 ");
    this.isSkip = true;
    this.skip.visible = false;
    for i = this.openCardIndex, #this.curShowCard do
        this.openCardIndex = this.openCardIndex + 1;
       print("抽卡index2 == ", this.openCardIndex);
        -- 播放单卡
        this.showCardBGSpine(cardBgItems[i]:GetChild(this.raceItems[i]).wrapTarget, this.starItems[i], cardBgItems[i]:GetChild("Effect_"..this.raceItems[i]), this.raceItems[i]);
        local cardItem = cardItems[i];
        cardItem:GetTransition(this.starItems[i]).ignoreEngineTimeScale = false;
        cardItem:GetTransition(this.starItems[i]):Play(function()
           print("抽卡显露出来 index2 == ", i);
            this.skipEnd(cardItem);
        end);
        cardBgItems[i]:GetTransition(this.raceItems[i]):Play();
        if this.isNewHero[i] then
            table.insert(this.needShowHeroList, this.isNewHero[i]);
        end
    end
    if this.showSkipEndTime ~= nil then
        utimer.remove(this.showSkipEndTime);
        this.showSkipEndTime = nil;
    end
    this.showSkipEndTime = utimer.timeCall(function()
        this.showSkipEndTime = nil;
        if this.curShowCard == nil then
            return true;
        end
        if this.openedCardCount ~= nil and this.openedCardCount < #this.curShowCard then
            this.openedCardCount = #this.curShowCard - 1;
            this.skipEnd();
        end
    end, 4000, 1);
end

-- 一键翻转后,单卡翻转完毕的回调
function UIDrawResult.skipEnd(cardItem)
    this.setCardItemAlpha(cardItem);
    this.openedCardCount = this.openedCardCount + 1;
    print("抽卡Count2 == ", this.openedCardCount);
    if this.openedCardCount == #this.curShowCard then
       print("抽卡翻转结束2 ");
        if this.showSkipEndTime ~= nil then
            utimer.remove(this.showSkipEndTime);
            this.showSkipEndTime = nil;
        end
        this.showAllNewHero();
        this.showConvert();
    end
end

function UIDrawResult.setCardItemAlpha(cardItem)
    if cardItem ~= nil then
        this.setCardImageAlpha(cardItem);
    else
        for i = 1, #this.curShowCard do
            local index = i;
            local localCardItem = cardItems[index];
            this.setCardImageAlpha(localCardItem);
        end
    end
end

function UIDrawResult.setCardImageAlpha(cardItem)
    if cardItem ~= nil then
        local imgQualityBg = cardItem:GetChild("qualityBg")
        local imgIcon = cardItem:GetChild("Icon")
        if imgQualityBg.visible == false then
            --logError("抽卡背景alpha设置不对");
            imgQualityBg.visible = true
        end
        if imgIcon.visible  == false then
            --logError("抽卡立绘alpha设置不对");
            imgIcon.visible = true
        end
        if cardItem.visible == false then
            --logError("抽卡卡片alpha设置不对");
            cardItem.visible = true
        end
    end
end

function UIDrawResult.showAllNewHero()
    if #this.needShowHeroList > 0 then
        print("抽卡新英雄展示2 ");
        this.showOneNewHero(this.showAllNewHero);
    else
        print("抽卡新英雄展示结束2 ");
        DestoryUI(PanelNames.UIDrawAnimation);
        print("抽卡显示再抽一次2 ");
        this.notAgain = true;
        -- if isOpenUI(PanelNames.UIDraw) then
        --     UIDraw.DelayEnableDraw();
        -- end
        if this.again then
            this.again.visible = true;
        end
        if aniReset then
            aniReset:Play();
        end
        UIDrawResult.RefreshShareCtr()
        logicMgr.GuideMgr.Resume()
        this.cheakDrawTips();
        --utimer.timeCall(function()
        --end, 300, 1);
    end
end

-- 播放新英雄
function UIDrawResult.showOneNewHero(callBack)
    print("抽卡新英雄展示列表长度: "..(#this.needShowHeroList));
    if #this.needShowHeroList > 0 then
        showUI(PanelNames.UIDrawAnimation, this.needShowHeroList[1], nil, false, callBack, nil, true);
        table.remove(this.needShowHeroList, 1);
    else
        if callBack then
            callBack();
        end
    end
end

-- 播放卡片转换资源
function UIDrawResult.showConvert()
    if this.drawBehavior.notGetHero then
        return
    end
    for i = 1, #this.curShowCard do
        if this.needResolveList[i] then
            cardItems[i]:GetTransition("showconvert"):Play();
        end
    end
end

function UIDrawResult.cardEndReset(fun)
    if this.resetTime then
        return ;
    end
    this.again.visible = false;
    this.cardReset:Play();
    aniReset:Stop();
    for i = 1, #this.curShowCard do
        cardItems[i]:GetTransition("reset"):Play();
        cardItems[i]:GetTransition("showconvert"):Stop();
        cardItems[i]:GetTransition("resetConvert"):Play();
    end
    this.resetTime = utimer.timeCall(function ()
        this.resetTime = nil;
        if fun then
            fun();
        end
    end, 33);
end

function UIDrawResult.onClickAllSkip()
    if logicMgr.HeroManager.getPlayerDrawIsShip() then
        this.againIsSkipController.selectedIndex = 0;
        logicMgr.HeroManager.setPlayerDrawIsShip("false");
    else
        this.againIsSkipController.selectedIndex = 1;
        logicMgr.HeroManager.setPlayerDrawIsShip("true");
    end
end

function UIDrawResult.OnClickShare()
    if this.curShowCard then
        if #this.curShowCard == 10 then
            showUI(PanelResNames.UIShareDraw, this.curShowCard)
        else
            showUI(PanelResNames.UIShareHero, {hero = this.curShowCard[1], type = 2})
        end
    end
end
---开通心愿,给个弹窗提示
function UIDrawResult.cheakDrawTips()
    local lastNum = logicMgr.HeroManager.GetLastDrawNum();
    local nowNum = logicMgr.HeroManager.GetWishNum();
    local wishNum = G.dataTable["t_global"]["406"].f_int_value;
    if lastNum and lastNum < wishNum and nowNum and nowNum >= wishNum then
        UISysTips.AddMsg(getLanguage("Draw_WishList_OpenTips"));
        logicMgr.HeroManager.SetLastDrawNum(nowNum);
    end
end
-----------------------------------连斩召唤----------开始----------------
function UIDrawResult.updateAgain()
    this.ssrNum = -1;
    this.againTypeController.selectedIndex = 2;
end

function UIDrawResult.onClickRecord()
    if logicMgr.ActiveManager.IsActiveEnd(this.typeParam) then
        this.onClickContinue();
        UISysTips.AddMsg(getLanguage("ErrorCode_3453"));
        return;
    end
    showUI(PanelNames.UISpecialDrawRecord, nil, this.typeParam, 2);
end
function UIDrawResult.TryToBuy()
    UISysTips.AddMsg(getLanguage("ActivityDesc1040101"))
end
function UIDrawResult.onClickSelectContinue()
    if logicMgr.ActiveManager.IsActiveEnd(this.typeParam) then
        this.onClickContinue();
        UISysTips.AddMsg(getLanguage("ErrorCode_3453"));
        return;
    end
    if this.openCardIndex <= #this.curShowCard then
        return ;
    end
    if this.ssrNum >= G.dataTable["t_global"][tostring(241)].f_int_value then
        local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop);
        pop.Init("33", function(b)
            if b then
                this.ssrNum = -1;
                this.onClickSelectContinue();
            end
        end, this.ssrNum);
        return
    end
    if this.selectDrawFinish then
        --local flag = true;
        --this.selectTableData, this.selectServerData = logicMgr.ActiveManager.getActiveData(this.typeParam);
        --if this.selectTableData ~= nil and this.selectServerData ~= nil then
        --    for k, v in pairs(this.selectServerData.param.selectDrawBox) do
        --        flag = false;
        --        break;
        --    end
        --    if flag then
        --        drawHandler.GoReqSelectDrawSaveMessage(tonumber(this.typeParam), 1);
        --    end
        --end
        ----this.onCloseUIDrawResult();
        --showUI(PanelNames.UISpecialDrawRecord, nil, this.typeParam, 1, flag);
        this.TryToBuy()
    else
        if not this.notAgain then
            return ;
        end
        this.notAgain = nil;
        this.cardEndReset(function ()
            drawHandler.GoReqSelectDrawRefreshMessage(this.typeParam);
            if this.drawAgainTime then
                utimer.remove(this.drawAgainTime);
                this.drawAgainTime = nil;
            end
            this.drawAgainTime = utimer.timeCall(function ()
                this.againTypeController.selectedIndex = 4;
            end , 3000, 1);
        end);
    end
end
-----------------------------------连斩召唤-----------结束---------------


return UIDrawResult;